/*
ID: pallab81
PROG:
LANG: C++
*/

//"I have not failed, I have just found 10000 ways that won't work.

#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <iomanip>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;

#define VI vector< int >
#define CI( x ) scanf("%d",&x)
#define CL( x ) scanf("%lld",&x)
#define CD( x ) scanf("%lf",&x )
#define foR(i,st,ed) for(int i = st ; i < ed ; ++i )
#define fo(i,ed) foR( i , 0 , ed )
#define foE(i,st,ed) for(int i = st ; i <= ed ; ++i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )

class ClosestRegex {
public:
    string closestString(string txt, string rgx);
};

const int inf = (int)1e9;
const string ss = string(52,'z');
string text;
string regix;
int ltext ;
int lregix ;
pair<int,string> dp[60][60];
int vis[60][60];

inline pair<int,string> ADD( pair<int,string> A, pair<int,string> B ) {
    A.f+= B.f;
    A.s+= B.s;
    return A;
}
inline pair<int,string> MIN( pair<int,string> A, pair<int,string> B ) {
    if ( A.f != B.f ) {
        return ( A.f < B.f ? A : B );
    }

    if ( LN(A.s) == LN(B.s) ) {
        return (A.s < B.s ? A : B );
    }
    return ( LN(A.s) < LN(B.s) ? A : B );
}


pair<int,string> dfs(int i, int j ) {
    if ( i == ltext && j== lregix) {
        return mk(0,"");
    }
    if ( j > lregix ) {
        return mk(inf,ss);
    }

    if ( vis[i][j]!=-1 ) {
        return dp[i][j];
    }
    vis[i][j]=1;

    pair<int,string> best = mk(inf,ss);

    if (  regix[j+1]!='*' ) {
        if ( regix[j]==text[i] ) {
            pair<int,string> a = mk(0,"");
            a.s+=regix[j];
            pair<int,string> b = dfs(i+1,j+1);
            best = MIN( best, ADD(a,b) );
        }
        else {
            pair<int,string> a = mk(1,"");
            a.s+=regix[j];
            pair<int,string> b = dfs(i+1,j+1);
            best = MIN( best, ADD(a,b) );
        }

    }
    else {
        // na niye
        pair<int,string> tmp = dfs(i,j+2);
        best = MIN(best, tmp );

        // 1ta or ter besi niye
        pair<int,string> a = mk(0,"");
        char c = regix[j];

        for (int k=i;k<ltext;++k) {
            if ( c==text[k] ) {
                a.s+=c;
                pair<int,string> b = dfs(k+1,j+2);
                best = MIN(best, ADD(a,b) );
            }
            else {
                a.s+=c;
                a.f+=1;
                pair<int,string> b = dfs(k+1,j+2);
                best = MIN(best, ADD(a,b) );
            }
        }
    }

    return dp[i][j]=best;
}
inline bool f1() {
    fo(i,lregix) {
        if ( regix[i]=='*' )return true;
    }
    return false;
}

string ClosestRegex :: closestString(string txt, string rgx) {


    text = txt;
    regix = rgx;

    ltext = LN( text );
    lregix = LN( regix );

    SET(vis,-1);
    regix+='~';
    pair<int,string> res = dfs(0,0);
    
//    cout<<res.f<<" "<<res.s<<"\n";

    if ( res.f >=inf ) {
        return "";
    }
    return res.s;
}
// BEGIN KAWIGIEDIT TESTING
// Generated by KawigiEdit 2.1.8 (beta) modified by pivanof
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool KawigiEdit_RunTest(int testNum, string p0, string p1, bool hasAnswer, string p2) {
    cout << "Test " << testNum << ": [" << "\"" << p0 << "\"" << "," << "\"" << p1 << "\"";
    cout << "]" << endl;
    ClosestRegex *obj;
    string answer;
    obj = new ClosestRegex();
    clock_t startTime = clock();
    answer = obj->closestString(p0, p1);
    clock_t endTime = clock();
    delete obj;
    bool res;
    res = true;
    cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
    if (hasAnswer) {
        cout << "Desired answer:" << endl;
        cout << "\t" << "\"" << p2 << "\"" << endl;
    }
    cout << "Your answer:" << endl;
    cout << "\t" << "\"" << answer << "\"" << endl;
    if (hasAnswer) {
        res = answer == p2;
    }
    if (!res) {
        cout << "DOESN'T MATCH!!!!" << endl;
    } else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
        cout << "FAIL the timeout" << endl;
        res = false;
    } else if (hasAnswer) {
        cout << "Match :-)" << endl;
    } else {
        cout << "OK, but is it right?" << endl;
    }
    cout << "" << endl;
    return res;
}
int main() {
    bool all_right;
    all_right = true;

    string p0;
    string p1;
    string p2;

    {
        // ----- test 0 -----
        p0 = "cbc";
        p1 = "a*b*";
        p2 = "abb";
        all_right = KawigiEdit_RunTest(0, p0, p1, true, p2) && all_right;
        // ------------------
    }

    {
        // ----- test 1 -----
        p0 = "topcoder";
        p1 = "t*px*coa*de*";
        p2 = "ttpcodee";
        all_right = KawigiEdit_RunTest(1, p0, p1, true, p2) && all_right;
        // ------------------
    }

    {
        // ----- test 2 -----
        p0 = "cmu";
        p1 = "c*m*fm*u*";
        p2 = "cfu";
        all_right = KawigiEdit_RunTest(2, p0, p1, true, p2) && all_right;
        // ------------------
    }

    {
        // ----- test 3 -----
        p0 = "aaaaacccc";
        p1 = "a*abc*";
        p2 = "aaaaabccc";
        all_right = KawigiEdit_RunTest(3, p0, p1, true, p2) && all_right;
        // ------------------
    }

    {
        // ----- test 4 -----
        p0 = "short";
        p1 = "lo*ts*of*let*ter*s";
        p2 = "";
        all_right = KawigiEdit_RunTest(4, p0, p1, true, p2) && all_right;
        // ------------------
    }

    if (all_right) {
        cout << "You're a stud (at least on the example cases)!" << endl;
    } else {
        cout << "Some of the test cases had errors." << endl;
    }
    return 0;
}
// END KAWIGIEDIT TESTING


//Powered by KawigiEdit 2.1.8 (beta) modified by pivanof!
// kate: indent-mode cstyle; space-indent on; indent-width 0; 
